class WebStorage {
  constructor(storageType = 'localStorage') {
    this.storage = window[storageType];
  }

  setItem(key, value) {
    try {
      const data = JSON.stringify(value);
      this.storage.setItem(key, data);
    } catch (error) {
      console.error('Failed to set item in storage:', error);
    }
  }

  getItem(key) {
    try {
      const data = this.storage.getItem(key);
      return data ? JSON.parse(data) : null;
    } catch (error) {
      console.error('Failed to get item from storage:', error);
      return null;
    }
  }

  removeItem(key) {
    this.storage.removeItem(key);
  }

  clear() {
    this.storage.clear();
  }

  hasItem(key) {
    return this.storage.getItem(key) !== null;
  }
}

export const localStore = new WebStorage('localStorage');
export const sessionStore = new WebStorage('sessionStorage');

export default WebStorage;